package ${webPackageName}.swagger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.async.DeferredResult;

import ${webPackageName}.constant.ScanPackageConstant;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import io.swagger.annotations.Api;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * SwaggerUI自动配置
 * @author lizihao
 *
 */
@Configuration
@EnableSwagger2
<#noparse>
public class SwaggerUiAutoConfiguration {


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .contact(new Contact("chinaunicom-base-framework", "http://127.0.0.1:8666/", "lleice@126.com"))
                .description("手脚架")
                .title("手脚架")
                .version("0.0.1-SNAPSHOT")
                .build();
    }


    @Bean
    public Docket createRestApi() {
    	 ParameterBuilder tokenPar = new ParameterBuilder();
         List<Parameter> pars = new ArrayList<Parameter>();
         tokenPar.name("Authorization").description("AccessToken令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
         pars.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .paths(PathSelectors.any())
                .apis(selectorApis())
                .build()
                .groupName("内部接口")
                .globalOperationParameters(pars)
                .apiInfo(apiInfo());
    }
    
    /**
     * 置顶扫描的包
     * @return
     */
    private Predicate<RequestHandler> selectorApis() {
        return Predicates.or(Arrays.asList(RequestHandlerSelectors.withClassAnnotation(Api.class)
                , RequestHandlerSelectors.basePackage(ScanPackageConstant.SWAGGER_ENTITY_SCAN)));
    }
}
</#noparse>